#include<bits/stdc++>
using namespace std;

// 【题目】力扣2824.统计和小于目标的下标对数目
// 【难度】简单
// 【提交】https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/submissions/669091124/?envType=problem-list-v2&envId=binary-search
// 【标签】数组；双指针；二分查找；排序

class Solution {
public:
    int countPairs(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int res = 0;
        for (int i = 1; i < nums.size(); i++) {
            int k = lower_bound(nums.begin(), nums.begin() + i, target - nums[i]) - nums.begin();
            res += k;
        }
        return res;
    }
};
